/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *MpmCustomerAnalysisController
 * 项目策划-客户决策链分析
 *
 * 修改纪录
 * 2021-08-09 版本：1.0 hudeyong 创建。
 * @version 版本：1.0
 * @author 作者：hudeyong
 * 创建日期：2021-08-09
 */

package com.topscomm.mpm.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.topscomm.mpm.pojo.MpmCustomerAnalysisEntity;
import com.topscomm.mpm.pojo.MpmCustomerTaskEntity;
import com.topscomm.mpm.pojo.MpmProjectPlanEntity;
import com.topscomm.mpm.service.impl.MpmCustomerTaskService;
import com.topscomm.mpm.service.impl.MpmProjectPlanService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.topscomm.mpm.controller.auto.MpmCustomerAnalysisControllerAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: hudeyong
 * @date: 2021-08-09
 * @description:项目策划-客户决策链分析Controller
 * @modify:
 */
@RestController
@RequestMapping("mpm/customerAnalysis")
public class MpmCustomerAnalysisController extends MpmCustomerAnalysisControllerAuto implements IMpmCustomerAnalysisController {
	@Autowired
	private MpmCustomerTaskService mpmCustomerTaskService;
	@Autowired
	private MpmProjectPlanService mpmProjectPlanService;
	/**
	 * @author: hudeyong
	 * @date: 2021-08-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_planid")) {
			String value = paraMap.get("qry_planid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and planid = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_taskid")) {
			String value = paraMap.get("qry_taskid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and customertaskid = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: hudeyong
	 * @date: 2021-08-09
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @author: houyuhang
	 * @date: 2022-03-10
	 * @param paraMap
	 * @return
	 * @description:重写平台list方法，获取客户任务和决策链数据
	 * @modify:
	 */
	@GetMapping(value = "/queryCustomerTaskList")
	public ResponseResult<Object> queryCustomerTaskList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellListSqlForCustomer(paraMap);
			Map<String, Object> dataMap = new HashMap<>();
			int begincount=query.getCurrentPage()*query.getPageSize()-query.getPageSize();
			dataMap.put("whereSql", sqlWhere);
			dataMap.put("begincount", begincount);
			dataMap.put("pagesize", query.getPageSize());
			dataMap.put("sidx", query.getSidx());
			dataMap.put("sord", query.getSord());
			int totalCount = this.service.getCount(dataMap,"customerTaskAnalysisGetCount");
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(dataMap, "customerTaskAnalysis");
			} else {
				tempList = this.service.queryMapByWhere(sqlWhere, "customerTaskAnalysisNoPage");
			}
			this.service.setDispFields(tempList);
			mpmCustomerTaskService.setDispFields(tempList);
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
	/**
	 * @author: houyuhang
	 * @date: 2021-03-10
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	protected String spellListSqlForCustomer(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_planid")) {
			String value = paraMap.get("qry_planid").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and MPMCUSTOMERTASK.planid = '{0}' ", value);
		}
		return sql.toString();
	}
	/**
	 * @author: houyuhang
	 * @date: 2022-03-17
	 * @param: paraMap
	 * @description: 系统部监管页面查询客户决策链分析信息
	 * @return: ResponseResult<Object>
	 * @modify:
	 */
	@GetMapping(value = "/queryCustomerAnalysisForSystem")
	public ResponseResult<Object> queryCustomerAnalysisForSystem(@RequestParam(required = false) Map<String, Object> paraMap) {
		String taskid = ConvertUtil.convertToString(paraMap.get("taskid"));
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" {0} in ({1}) ", MpmCustomerAnalysisEntity.FieldCustomertaskid ,taskid);
		List<Map<String, Object>> mpmCustomerAnalysisEntities = this.service.queryMapByWhere(sql.toString());
		if(CollectionUtils.isEmpty(mpmCustomerAnalysisEntities)) {
			return ResponseResult.ok(new HashMap<>());
		}
		Map<String, List<Map<String, Object>>> mpmCustomerAnalysisMap = mpmCustomerAnalysisEntities.stream()
				.collect(Collectors.groupingBy(item -> ConvertUtil.convertToString(item.get("customertaskid"))));
		return ResponseResult.ok(mpmCustomerAnalysisMap);
	}
}

